package com.neu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neu.base.PageBean;
import com.neu.entity.ProgressRecord;
import com.neu.entity.Project;
import com.neu.entity.ReportInfo;
import com.neu.entity.Review;
import com.neu.mapper.ProgressRecordMapper;
import com.neu.mapper.ReportInfoMapper;
import com.neu.service.ProgressRecordService;
import com.neu.service.ProjectService;
import com.neu.service.ReportInfoService;
import com.neu.service.TenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ReportInfoServiceImpl extends ServiceImpl<ReportInfoMapper, ReportInfo> implements ReportInfoService {
    @Autowired
    private ReportInfoMapper reportInfoMapper;

    @Autowired
    private ProjectService projectService;

    @Autowired
    private TenantServiceImpl tenantService;

    @Override
    public ReportInfo getByProjectId(String projectId) {
        LambdaQueryWrapper<ReportInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ReportInfo::getProjectId, projectId);
        return reportInfoMapper.selectOne(queryWrapper);
    }

    @Override
    public Map<String, Object> search(Map map){
        String tenantId = map.get("tenantId").toString();
        String projectName = null;
        LocalDate startTime = null;
        LocalDate endTime = null;
        if (map.get("projectName") != null && !StringUtils.isEmpty(map.get("projectName").toString())){
            projectName = map.get("projectName").toString();
        }
        if (map.get("startTime") != null && !StringUtils.isEmpty(map.get("startTime").toString())){
            startTime = LocalDate.parse(map.get("startTime").toString());
        }
        if (map.get("endTime") != null && !StringUtils.isEmpty(map.get("endTime").toString())){
            endTime = LocalDate.parse(map.get("endTime").toString());
        }

        List<Project> projectList = projectService.list();
        List<Project> selectProjectList = new ArrayList<>();
        for (Project project : projectList) {
            if (project.getTenantId().equals(tenantId) && project.getProjectStatus().equals("2") && (projectName == null || project.getProjectName().equals(projectName)) &&
                    (startTime == null || startTime.isBefore(project.getUpdateTime().toLocalDate()) || startTime.isEqual(project.getUpdateTime().toLocalDate())) &&
                    (endTime == null || endTime.isAfter(project.getUpdateTime().toLocalDate()) || endTime.isEqual(project.getUpdateTime().toLocalDate()))) {
                selectProjectList.add(project);
            }
        }

        List<String> tenantNames = new ArrayList<>();
        for (Project project : selectProjectList) {
            String tenantName = tenantService.getById(project.getTenantId()).getTenantName();
            tenantNames.add(tenantName);
        }

        Map<String, Object> result = new HashMap<>();
        result.put("projects", selectProjectList);
        result.put("tenantNames", tenantNames);

        return result;
    }
}
